<?php
// +----------------------------------------------------------------------
// | Fanwe 方维p2p借贷系统
// +----------------------------------------------------------------------
// | Copyright (c) 2011 http://www.fanwe.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 云淡风轻(88522820@qq.com)
// +----------------------------------------------------------------------

class synModule  extends baseModule
{
	//登录 test
	public function login()
	{
		if(IS_DEBUG){
			$mobile = intval($_REQUEST['mobile']);
			$uid = intval($_REQUEST['id']);
			if($mobile){
				$user_data = $GLOBALS['db']->getRow("select * from ".DB_PREFIX."user where mobile =".$mobile);
			}else{
				if($uid){
					$user_data = $GLOBALS['db']->getRow("select * from ".DB_PREFIX."user where id =".$uid);
				}else{
					print_r("请填写会员ID");exit;
				}
			}
	
			es_session::set("user_info",$user_data);
			$GLOBALS['user_info'] = $user_data;
			es_cookie::set("client_ip",CLIENT_IP,3600*24*30);
			es_cookie::set("nick_name",$user_data['nick_name'],3600*24*30);
			es_cookie::set("user_id",$user_data['id'],3600*24*30);
			es_cookie::set("user_pwd",md5($user_data['user_pwd']."_EASE_COOKIE"),3600*24*30);
			es_cookie::set("is_agree",$user_data['is_agree'],3600*24*30);
			es_cookie::set("PHPSESSID2",es_session::id(),3600*24*30);
			ajax_return($user_data);
		}
		
	}
	//循环同步到IM
	function  synchronization_im(){
		if(IS_DEBUG){
			fanwe_require(APP_ROOT_PATH."system/libs/user.php");
			$user_id = intval($_REQUEST['id']);
			if($user_id){
				$user = $GLOBALS['db']->getAll("SELECT id,nick_name,head_image FROM ".DB_PREFIX."user where id = ".$user_id);
			}else{
				$user = $GLOBALS['db']->getAll("SELECT id,nick_name,head_image FROM ".DB_PREFIX."user where synchronize=0 ");
			}
			if($user){
				foreach($user as $k=>$user_data){
					accountimport($user_data);
				}
				echo $user_id."用户已同步";
			}else{
				echo "用户已同步";
			}
		}
	}
	//循环同步到redis
	function  synchronization_redis(){
		if(IS_DEBUG){
			fanwe_require(APP_ROOT_PATH.'mapi/lib/redis/UserRedisService.php');
			$user_redis = new UserRedisService();
			$user_id = intval($_REQUEST['id']);
			$type = intval($_REQUEST['type']);
			if($user_id){
				if($type==1&&$_REQUEST['c']!=''){
					fanwe_require(APP_ROOT_PATH."system/libs/user.php");
					$user_info =array();
					if($_REQUEST['c']=='mobile'||$_REQUEST['c']=='all')
						$user_info['mobile'] ='';
	
					if($_REQUEST['c']=='qq'||$_REQUEST['c']=='all')
						$user_info['qq_openid'] ='';
	
					if($_REQUEST['c']=='wx'||$_REQUEST['c']=='all'){
						$user_info['wx_openid'] ='';
						$user_info['wx_unionid'] ='';
					}
					if($_REQUEST['c']=='sina'||$_REQUEST['c']=='all'){
						$user_info['sina_id'] ='';
					}
	
					if($_REQUEST['c']=='wx_gz'||$_REQUEST['c']=='all'){
						$user_info['gz_openid'] ='';
						$user_info['subscribe'] ='';
					}
	
					$where = "id=".intval($user_id);
					$GLOBALS['db']->autoExecute(DB_PREFIX."user",$user_info,'UPDATE',$where);
				}
	
				$user = $GLOBALS['db']->getRow("SELECT * FROM ".DB_PREFIX."user where id = ".$user_id);
				if($user){
					$user_redis->update_db($user_id,$user);
					echo "用户redis同步完成";
				}else{
					echo "用户redis已同步";
				}
			}
		}
	}
	//图片同步
	function  synchronization_image(){
		if(IS_DEBUG){
			$user_id = intval($_REQUEST['id']);
			if($user_id){
				$user_data = $GLOBALS['db']->getRow("select id,head_image from ".DB_PREFIX."user where id =".$user_id);
	
				if($GLOBALS['distribution_cfg']['OSS_TYPE']&&$GLOBALS['distribution_cfg']['OSS_TYPE']!='NONE')
				{
					syn_to_remote_image_server($user_data['head_image']);
				}
	
				echo "执行结束";exit;
			}else{
				echo "ID空";exit;
			}
		}
	}
	//测试读取缓存 load_auto_cache
	public function m_configs(){
		if(IS_DEBUG){
			$cache_name = $_REQUEST['cache_name'];
			$rm_cache = intval($_REQUEST['rm_cache']);
			if($rm_cache){
				rm_auto_cache($cache_name);
			}
			$cache =  load_auto_cache($cache_name);
			print_r($cache);exit;
		}
	}
	function get_ip(){
		print_r(get_info());
	}

    //删除定时器加入直播的机器人列表
    public function del_user_robot(){
        if(IS_DEBUG){
            fanwe_require(APP_ROOT_PATH.'mapi/lib/redis/BaseRedisService.php');
            fanwe_require(APP_ROOT_PATH.'mapi/lib/redis/UserRedisService.php');
            $user_redis = new UserRedisService();
            $video_con_keys = $user_redis->redis->keys($GLOBALS['distribution_cfg']['REDIS_PREFIX'].'user_robot');
            $video_con_count = $user_redis->redis->delete($video_con_keys);
            print_r($video_con_count);exit;
        }
    }

    //同步机器人到redis
    public function robot(){
        if(IS_DEBUG){
            $user_data = $GLOBALS['db']->getAll("select * from ".DB_PREFIX."user where is_robot = 1");
            fanwe_require(APP_ROOT_PATH.'mapi/lib/redis/BaseRedisService.php');
            fanwe_require(APP_ROOT_PATH.'mapi/lib/redis/UserRedisService.php');
            $user_redis = new UserRedisService();
            foreach($user_data as $k=>$v){
                $user_redis->insert_db($v['id'],$v);
                $ret[] = $v['id'];
            }
            print_r($ret);exit;
        }

    }

    //同步机器人到im
    public function robot_im(){
        if(IS_DEBUG){
            $user_data = $GLOBALS['db']->getAll("select * from ".DB_PREFIX."user where is_robot = 1");
            require_once(APP_ROOT_PATH.'system/tim/TimApi.php');
            fanwe_require(APP_ROOT_PATH.'mapi/lib/redis/BaseRedisService.php');
            fanwe_require(APP_ROOT_PATH.'mapi/lib/redis/UserRedisService.php');
            $user_redis = new UserRedisService();
            $api = createTimAPI();
            foreach($user_data as $k=>$v){

                //添加成功，同步信息

                $ret = $api->account_import((string)$v['id'], $v['nick_name'], $v['head_image']);
                if($ret['ErrorCode']==0){
                    $GLOBALS['db']->query("update ".DB_PREFIX."user set synchronize = 1 where id =".$v['id']);
                    $data['synchronize'] = 1;
                    $user_redis->update_db($v['id'],$data);
                    print_r($v['id']);
                    print_r("  ");
                }
            }
            exit;
        }
    }

    //同步某个用户信息到redis
    public function update_user(){
        if(IS_DEBUG){
            $id = $_REQUEST['id'];
            $user_data = $GLOBALS['db']->getRow("select * from ".DB_PREFIX."user where id = ".$id);
            fanwe_require(APP_ROOT_PATH.'mapi/lib/redis/BaseRedisService.php');
            fanwe_require(APP_ROOT_PATH.'mapi/lib/redis/UserRedisService.php');
            $user_redis = new UserRedisService();
            $user_redis->update_db($user_data['id'],$user_data);
        }
    }
    
    public function pay_live(){
    	if(IS_DEBUG){
	    	//扣费开始
	 		$m_config =  load_auto_cache("m_config");//初始化手机端配置 
	 		$uesddiamonds_to_score = $m_config['uesddiamonds_to_score'];
	 		$ticket_to_rate = $m_config['ticket_to_rate'];
	 		
	 		$sql = "select id,from_user_id,to_user_id,total_ticket from ".DB_PREFIX."live_pay_log  where create_date = '2017-01-18' order by id desc";
			$live_pay_log_info = $GLOBALS['db']->getAll($sql);
	 		
	 		foreach($live_pay_log_info as $k=>$v){
	 			//$total_ticket = round($v['live_fee']*floatval($ticket_to_rate),2);
	 			//echo $v['id']."--".$v['to_user_id']."--".$v['total_ticket'];echo "<hr/>";
	 			//echo $v['to_user_id'].",";
	 			//$sql = "update ".DB_PREFIX."live_pay_log set total_ticket = ".$total_ticket." where total_ticket=0 and id = ".$v['id'];
				//$GLOBALS['db']->query($sql);
	 			//if($GLOBALS['db']->affected_rows()){
	 				//$sql = "update ".DB_PREFIX."user set score = score + ".$v['total_score']." where id = ".$v['from_user_id'];
					//$GLOBALS['db']->query($sql);
	 				//$sql = "update ".DB_PREFIX."user set ticket = ticket + ".$v['total_ticket']." where id = ".$v['to_user_id'];
					//$GLOBALS['db']->query($sql);
					//user_deal_to_reids(array($v['from_user_id']));//同步user信息到redis
					//更新主播直播间获得印票
					fanwe_require(APP_ROOT_PATH.'mapi/lib/redis/VideoContributionRedisService.php');
	                $videoCont_redis = new VideoContributionRedisService();
	                $videoCont_redis->insert_db($v['from_user_id'], $v['to_user_id'],$v['id'], $v['total_ticket']);
	 			//}
	 			
	 		}
	 		
	 		print_r(count($live_pay_log_info));exit;
	 		/**/
    	}
    	
    }
}


?>
